//
//  BCHomePageVC.m
//  BangcaiSale
//
//  Created by lsy on 16/7/7.
//  Copyright © 2016年 Bangcai. All rights reserved.
//

#import "BCHomePageVC.h"
#import "BCChartView.h"
#import "BCMessageViewController.h"

@interface BCHomePageVC ()
@property (nonatomic, strong) NSString *p_Title;
@end

@implementation BCHomePageVC

-(void)viewDidLoad {
    [super viewDidLoad];
    [self p_initNavgationBar];
    [self p_initMenuView];
}

-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //隐藏导航栏
    self.navigationController.navigationBar.hidden = YES;
    
    if (self.rdv_tabBarController.tabBarHidden == YES) {
        [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
    }
}

-(void)viewWillDisappear:(BOOL)animated {
}

#pragma mark - View init
-(void)p_initNavgationBar {
    _p_Title = @"大连棒棰岛肉联直营店";
    self.view.backgroundColor = BangcaiBlue;
    UIImageView *leftItemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, 35.f, 21.f, 21.f)];
    leftItemImageView.image = [UIImage imageNamed:@"消息"];
    [self.view addSubview:leftItemImageView];
    
    UIButton *msgButton = [UIButton buttonWithType:UIButtonTypeCustom];
    msgButton.frame = CGRectMake(0, 0, 72.f, 56.f);
    msgButton.backgroundColor = [UIColor clearColor];
    [msgButton addTarget:self action:@selector(msgAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:msgButton];
    
    UIImageView *rightItemImageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-10.f-72.f, 35.f, 71.f, 21.f)];
    rightItemImageView.image = [UIImage imageNamed:@"上线"];
    [self.view addSubview:rightItemImageView];
    
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    rightButton.frame = CGRectMake(SCREEN_WIDTH-82.f, 0, 82.f, 56.f);
    rightButton.backgroundColor = [UIColor clearColor];
    [rightButton addTarget:self action:@selector(rightAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:rightButton];
    
    UILabel *rightLable = [[UILabel alloc] initWithFrame:CGRectMake(rightItemImageView.frame.origin.x+10.f, 35.f, rightItemImageView.frame.size.width-20.f, rightItemImageView.frame.size.height)];
    rightLable.textColor = [UIColor whiteColor];
    rightLable.font = [UIFont boldSystemFontOfSize:12.f];
    rightLable.text = @"我要接单";
    rightLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:rightLable];
    
    UILabel *titleLable = [[UILabel alloc] initWithFrame:CGRectMake(82.f, 35.f, SCREEN_WIDTH-(82.f*2), 21.f)];
    titleLable.backgroundColor = [UIColor clearColor];
    titleLable.textColor = [UIColor whiteColor];
    titleLable.font = [UIFont boldSystemFontOfSize:18.f];
    if (iPhone4_4S || iPhone5_C_SE) {
        titleLable.font = [UIFont boldSystemFontOfSize:15.f];
    }
    titleLable.text = _p_Title;
    titleLable.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:titleLable];
    
    CGFloat msgReddotWidth = 10.f;
    UIView *msgReddotView = [[UIView alloc] initWithFrame:CGRectMake(36.f-msgReddotWidth, 32.f, msgReddotWidth, msgReddotWidth)];
    msgReddotView.layer.cornerRadius = msgReddotView.frame.size.height*0.5f;
    msgReddotView.backgroundColor = [UIColor redColor];
    [self.view addSubview:msgReddotView];
}

- (void)p_initMenuView {
    BCChartView *chartView = [[BCChartView alloc] initWithFrame:CGRectMake(0, 55, SCREEN_WIDTH, SCREEN_HEIGHT*0.34f)];
    [self.view addSubview:chartView];
    
    UIView *middleBarView = [[UIView alloc] initWithFrame:CGRectMake(0, chartView.frame.origin.y+chartView.frame.size.height, SCREEN_WIDTH, 40.f)];
    middleBarView.backgroundColor = [UIColor colorWithRed:0.227 green:0.325 blue:0.420 alpha:1.000];
    [self.view addSubview:middleBarView];
    
    UILabel *middleBarTitleLable = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 0, 61.f, middleBarView.frame.size.height)];
    middleBarTitleLable.backgroundColor = [UIColor clearColor];
    middleBarTitleLable.textColor = [UIColor whiteColor];
    middleBarTitleLable.font = [UIFont systemFontOfSize:15.f];
    middleBarTitleLable.text = @"收入总额";
    middleBarTitleLable.textAlignment = NSTextAlignmentLeft;
    [middleBarView addSubview:middleBarTitleLable];
    
    UILabel *middleBarSumLable = [[UILabel alloc] initWithFrame:CGRectMake(middleBarTitleLable.frame.origin.x+middleBarTitleLable.frame.size.width, 0, SCREEN_WIDTH-middleBarTitleLable.frame.origin.x-middleBarTitleLable.frame.size.width-15.f, middleBarView.frame.size.height)];
    middleBarSumLable.backgroundColor = [UIColor clearColor];
    middleBarSumLable.textColor = [UIColor whiteColor];
    middleBarSumLable.font = [UIFont systemFontOfSize:18.f];
    middleBarSumLable.text = @"￥2345.50";
    middleBarSumLable.textAlignment = NSTextAlignmentRight;
    [middleBarView addSubview:middleBarSumLable];
    
    CGFloat menu_w = (SCREEN_WIDTH-(15.f*3))*0.5f;
    CGFloat menu_h = ((SCREEN_HEIGHT-middleBarView.frame.origin.y-middleBarView.frame.size.height-self.rdv_tabBarController.tabBar.frame.size.height)-(15.f*3))*0.5f;
    for (int i=0; i<4; i++) {
        if (0==i) {
            UIButton *menuButton_ul = [UIButton buttonWithType:UIButtonTypeCustom];
            menuButton_ul.frame = CGRectMake(15.f, middleBarView.frame.origin.y+middleBarView.frame.size.height+15.f, menu_w, menu_h);
            [menuButton_ul setBackgroundColor:[UIColor colorWithRed:0.392 green:0.839 blue:0.553 alpha:1.000]];
            menuButton_ul.layer.cornerRadius = 8.f;
            menuButton_ul.tag = 66660;
            [menuButton_ul addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:menuButton_ul];
            
            UILabel *menuTitleLable_ul = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 15.f, menu_w-30.f, 18.f)];
            menuTitleLable_ul.textColor = [UIColor whiteColor];
            menuTitleLable_ul.font = [UIFont systemFontOfSize:18.f];
            menuTitleLable_ul.text = @"在售商品";
            menuTitleLable_ul.textAlignment = NSTextAlignmentLeft;
            [menuButton_ul addSubview:menuTitleLable_ul];
            
            UIImageView *menuIconImageView_ul = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, menu_h-43.f-15.f, 43.f, 43.f)];
            menuIconImageView_ul.image = [UIImage imageNamed:@"在售商品"];
            [menuButton_ul addSubview:menuIconImageView_ul];
            
            UILabel *menuDetailLable_ul = [[UILabel alloc] initWithFrame:CGRectMake(menu_w-(menu_w-menuIconImageView_ul.frame.size.width-30.f)-15.f, menuIconImageView_ul.frame.origin.y+10.f, menu_w-menuIconImageView_ul.frame.size.width-30.f, 35.f)];
            menuDetailLable_ul.textColor = [UIColor whiteColor];
            menuDetailLable_ul.font = [UIFont systemFontOfSize:35.f];
            menuDetailLable_ul.text = @"32";
            menuDetailLable_ul.textAlignment = NSTextAlignmentRight;
            [menuButton_ul addSubview:menuDetailLable_ul];
            
            if (iPhone5_C_SE) {
                menuTitleLable_ul.font = [UIFont systemFontOfSize:15.f];
                CGRect frame = menuIconImageView_ul.frame;
                frame.origin.y = frame.origin.y+15.f;
                frame.size.width = 35.f;
                frame.size.height = frame.size.width;
                menuIconImageView_ul.frame = frame;
                menuDetailLable_ul.font = [UIFont systemFontOfSize:24.f];
                menuDetailLable_ul.frame = CGRectMake(menuDetailLable_ul.frame.origin.x,
                                                      menuDetailLable_ul.frame.origin.y+20.f,
                                                      menuDetailLable_ul.frame.size.width,
                                                      24.f);
            }
            
            if (iPhone4_4S) {
                menuTitleLable_ul.font = [UIFont systemFontOfSize:12.f];
                menuTitleLable_ul.frame = CGRectMake(menuTitleLable_ul.frame.origin.x,
                                                     menuTitleLable_ul.frame.origin.y-10.f,
                                                     menuTitleLable_ul.frame.size.width,
                                                     menuTitleLable_ul.frame.size.height);
                CGRect frame = menuIconImageView_ul.frame;
                frame.origin.y = frame.origin.y+23.f;
                frame.size.width = 28.f;
                frame.size.height = frame.size.width;
                menuIconImageView_ul.frame = frame;
                menuDetailLable_ul.font = [UIFont systemFontOfSize:18.f];
                menuDetailLable_ul.frame = CGRectMake(menuDetailLable_ul.frame.origin.x,
                                                      menuDetailLable_ul.frame.origin.y+25.f,
                                                      menuDetailLable_ul.frame.size.width,
                                                      18);
            }
        }
        else if (1==i) {
            UIButton *menuButton_ur = [UIButton buttonWithType:UIButtonTypeCustom];
            menuButton_ur.frame = CGRectMake(15.f+(menu_w)+15.f, middleBarView.frame.origin.y+middleBarView.frame.size.height+15.f, menu_w, menu_h);
            [menuButton_ur setBackgroundColor:[UIColor colorWithRed:0.965 green:0.675 blue:0.196 alpha:1.000]];
            menuButton_ur.layer.cornerRadius = 8.f;
            menuButton_ur.tag = 66661;
            [menuButton_ur addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:menuButton_ur];
            
            UILabel *menuTitleLable_ur = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 15.f, menu_w-30.f, 18.f)];
            menuTitleLable_ur.textColor = [UIColor whiteColor];
            menuTitleLable_ur.font = [UIFont systemFontOfSize:18.f];
            menuTitleLable_ur.text = @"待发货";
            menuTitleLable_ur.textAlignment = NSTextAlignmentLeft;
            [menuButton_ur addSubview:menuTitleLable_ur];
            
            UIImageView *menuIconImageView_ur = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, menu_h-43.f-15.f, 43.f, 43.f)];
            menuIconImageView_ur.image = [UIImage imageNamed:@"待发货"];
            [menuButton_ur addSubview:menuIconImageView_ur];
            
            UILabel *menuDetailLable_ur = [[UILabel alloc] initWithFrame:CGRectMake(menu_w-(menu_w-menuIconImageView_ur.frame.size.width-30.f)-15.f, menuIconImageView_ur.frame.origin.y+10.f, menu_w-menuIconImageView_ur.frame.size.width-30.f, 35.f)];
            menuDetailLable_ur.textColor = [UIColor whiteColor];
            menuDetailLable_ur.font = [UIFont systemFontOfSize:35.f];
            menuDetailLable_ur.text = @"8";
            menuDetailLable_ur.textAlignment = NSTextAlignmentRight;
            [menuButton_ur addSubview:menuDetailLable_ur];
            
            if (iPhone5_C_SE) {
                menuTitleLable_ur.font = [UIFont systemFontOfSize:15.f];
                CGRect frame = menuIconImageView_ur.frame;
                frame.origin.y = frame.origin.y+15.f;
                frame.size.width = 35.f;
                frame.size.height = frame.size.width;
                menuIconImageView_ur.frame = frame;
                menuDetailLable_ur.font = [UIFont systemFontOfSize:24.f];
                menuDetailLable_ur.frame = CGRectMake(menuDetailLable_ur.frame.origin.x,
                                                      menuDetailLable_ur.frame.origin.y+20.f,
                                                      menuDetailLable_ur.frame.size.width,
                                                      24.f);
            }
            
            if (iPhone4_4S) {
                menuTitleLable_ur.font = [UIFont systemFontOfSize:12.f];
                menuTitleLable_ur.frame = CGRectMake(menuTitleLable_ur.frame.origin.x,
                                                     menuTitleLable_ur.frame.origin.y-10.f,
                                                     menuTitleLable_ur.frame.size.width,
                                                     menuTitleLable_ur.frame.size.height);
                CGRect frame = menuIconImageView_ur.frame;
                frame.origin.y = frame.origin.y+23.f;
                frame.size.width = 28.f;
                frame.size.height = frame.size.width;
                menuIconImageView_ur.frame = frame;
                menuDetailLable_ur.font = [UIFont systemFontOfSize:18.f];
                menuDetailLable_ur.frame = CGRectMake(menuDetailLable_ur.frame.origin.x,
                                                      menuDetailLable_ur.frame.origin.y+25.f,
                                                      menuDetailLable_ur.frame.size.width,
                                                      18);
            }
        }
        else if (2==i) {
            UIButton *menuButton_dl = [UIButton buttonWithType:UIButtonTypeCustom];
            menuButton_dl.frame = CGRectMake(15.f, middleBarView.frame.origin.y+middleBarView.frame.size.height+15.f+(menu_h)+15.f, menu_w, menu_h);
            [menuButton_dl setBackgroundColor:[UIColor colorWithRed:0.933 green:0.322 blue:0.263 alpha:1.000]];
            menuButton_dl.layer.cornerRadius = 8.f;
            menuButton_dl.tag = 66662;
            [menuButton_dl addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:menuButton_dl];
            
            UILabel *menuTitleLable_dl = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 15.f, menu_w-30.f, 18.f)];
            menuTitleLable_dl.textColor = [UIColor whiteColor];
            menuTitleLable_dl.font = [UIFont systemFontOfSize:18.f];
            menuTitleLable_dl.text = @"本周收入";
            menuTitleLable_dl.textAlignment = NSTextAlignmentLeft;
            [menuButton_dl addSubview:menuTitleLable_dl];
            
            UIImageView *menuIconImageView_dl = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, menu_h-43.f-15.f, 43.f, 43.f)];
            menuIconImageView_dl.image = [UIImage imageNamed:@"本周"];
            [menuButton_dl addSubview:menuIconImageView_dl];
            
            UILabel *menuDetailLable_dl = [[UILabel alloc] initWithFrame:CGRectMake(menu_w-(menu_w-menuIconImageView_dl.frame.size.width-30.f)-15.f, menuIconImageView_dl.frame.origin.y+28.f, menu_w-menuIconImageView_dl.frame.size.width-30.f, 15.f)];
            menuDetailLable_dl.textColor = [UIColor whiteColor];
            menuDetailLable_dl.font = [UIFont systemFontOfSize:15.f];
            menuDetailLable_dl.text = @"￥32124.00";
            menuDetailLable_dl.textAlignment = NSTextAlignmentRight;
            [menuButton_dl addSubview:menuDetailLable_dl];
            
            if (iPhone5_C_SE) {
                menuTitleLable_dl.font = [UIFont systemFontOfSize:15.f];
                CGRect menuIconImageFrame = menuIconImageView_dl.frame;
                menuIconImageFrame.origin.y = menuIconImageFrame.origin.y+13.f;
                menuIconImageFrame.size.width = 35.f;
                menuIconImageFrame.size.height = menuIconImageFrame.size.width;
                menuIconImageView_dl.frame = menuIconImageFrame;
                CGRect menuDetailFrame = menuDetailLable_dl.frame;
                menuDetailFrame.origin.y = menuDetailFrame.origin.y-5.f;
                menuDetailLable_dl.frame = menuDetailFrame;
                menuDetailLable_dl.font = [UIFont systemFontOfSize:11.f];
                menuDetailLable_dl.frame = CGRectMake(menuDetailLable_dl.frame.origin.x,
                                                      menuDetailLable_dl.frame.origin.y+13.f,
                                                      menuDetailLable_dl.frame.size.width,
                                                      11);
            }
            
            if (iPhone4_4S) {
                menuTitleLable_dl.font = [UIFont systemFontOfSize:12.f];
                menuTitleLable_dl.frame = CGRectMake(menuTitleLable_dl.frame.origin.x,
                                                     menuTitleLable_dl.frame.origin.y-10.f,
                                                     menuTitleLable_dl.frame.size.width,
                                                     menuTitleLable_dl.frame.size.height);
                CGRect menuIconImageFrame = menuIconImageView_dl.frame;
                menuIconImageFrame.origin.y = menuIconImageFrame.origin.y+23.f;
                menuIconImageFrame.size.width = 28.f;
                menuIconImageFrame.size.height = menuIconImageFrame.size.width;
                menuIconImageView_dl.frame = menuIconImageFrame;
                CGRect menuDetailFrame = menuDetailLable_dl.frame;
                menuDetailFrame.origin.y = menuDetailFrame.origin.y-5.f;
                menuDetailLable_dl.frame = menuDetailFrame;
                menuDetailLable_dl.font = [UIFont systemFontOfSize:11.f];
                menuDetailLable_dl.frame = CGRectMake(menuDetailLable_dl.frame.origin.x,
                                                      menuDetailLable_dl.frame.origin.y+17.f,
                                                      menuDetailLable_dl.frame.size.width,
                                                      11);
            }
        }
        else {
            UIButton *menuButton_dr = [UIButton buttonWithType:UIButtonTypeCustom];
            menuButton_dr.frame = CGRectMake(15.f+(menu_w)+15.f, middleBarView.frame.origin.y+middleBarView.frame.size.height+15.f+(menu_h)+15.f, menu_w, menu_h);
            [menuButton_dr setBackgroundColor:[UIColor colorWithRed:0.204 green:0.596 blue:0.859 alpha:1.000]];
            menuButton_dr.layer.cornerRadius = 8.f;
            menuButton_dr.tag = 66663;
            [menuButton_dr addTarget:self action:@selector(menuAction:) forControlEvents:UIControlEventTouchUpInside];
            [self.view addSubview:menuButton_dr];
            
            UILabel *menuTitleLable_dr = [[UILabel alloc] initWithFrame:CGRectMake(15.f, 15.f, menu_w-30.f, 18.f)];
            menuTitleLable_dr.textColor = [UIColor whiteColor];
            menuTitleLable_dr.font = [UIFont systemFontOfSize:18.f];
            menuTitleLable_dr.text = @"订单总数";
            menuTitleLable_dr.textAlignment = NSTextAlignmentLeft;
            [menuButton_dr addSubview:menuTitleLable_dr];
            
            UIImageView *menuIconImageView_dr = [[UIImageView alloc] initWithFrame:CGRectMake(15.f, menu_h-43.f-15.f, 43.f, 43.f)];
            menuIconImageView_dr.image = [UIImage imageNamed:@"总订单数"];
            [menuButton_dr addSubview:menuIconImageView_dr];
            
            UILabel *menuDetailLable_dr = [[UILabel alloc] initWithFrame:CGRectMake(menu_w-(menu_w-menuIconImageView_dr.frame.size.width-30.f)-15.f, menuIconImageView_dr.frame.origin.y+10.f, menu_w-menuIconImageView_dr.frame.size.width-30.f, 35.f)];
            menuDetailLable_dr.textColor = [UIColor whiteColor];
            menuDetailLable_dr.font = [UIFont systemFontOfSize:35.f];
            menuDetailLable_dr.text = @"8";
            menuDetailLable_dr.textAlignment = NSTextAlignmentRight;
            [menuButton_dr addSubview:menuDetailLable_dr];
            
            if (iPhone5_C_SE) {
                menuTitleLable_dr.font = [UIFont systemFontOfSize:15.f];
                CGRect frame = menuIconImageView_dr.frame;
                frame.origin.y = frame.origin.y+15.f;
                frame.size.width = 35.f;
                frame.size.height = frame.size.width;
                menuIconImageView_dr.frame = frame;
                menuDetailLable_dr.font = [UIFont systemFontOfSize:24.f];
                menuDetailLable_dr.frame = CGRectMake(menuDetailLable_dr.frame.origin.x,
                                                      menuDetailLable_dr.frame.origin.y+20.f,
                                                      menuDetailLable_dr.frame.size.width,
                                                      24.f);
            }
            
            if (iPhone4_4S) {
                menuTitleLable_dr.font = [UIFont systemFontOfSize:12.f];
                menuTitleLable_dr.frame = CGRectMake(menuTitleLable_dr.frame.origin.x,
                                                     menuTitleLable_dr.frame.origin.y-10.f,
                                                     menuTitleLable_dr.frame.size.width,
                                                     menuTitleLable_dr.frame.size.height);
                CGRect frame = menuIconImageView_dr.frame;
                frame.origin.y = frame.origin.y+23.f;
                frame.size.width = 28.f;
                frame.size.height = frame.size.width;
                menuIconImageView_dr.frame = frame;
                menuDetailLable_dr.font = [UIFont systemFontOfSize:18.f];
                menuDetailLable_dr.frame = CGRectMake(menuDetailLable_dr.frame.origin.x,
                                                      menuDetailLable_dr.frame.origin.y+25.f,
                                                      menuDetailLable_dr.frame.size.width,
                                                      18);
            }
        }
    }
}

#pragma mark - Private Action

-(void)menuAction:(UIButton *)sender {
    switch (sender.tag) {
        case 66660:
        {
            //在售商品
            [self.rdv_tabBarController setSelectedIndex:2];
        }
            break;
        case 66661:
        {
            //待发货
            [self.rdv_tabBarController setSelectedIndex:1];
            [[NSUserDefaults standardUserDefaults] setValue:@(YES) forKey:@"isFromMianPage"];
        }
            break;
        case 66662:
        {
            //本周收入
            [self.rdv_tabBarController setSelectedIndex:3];
            [[NSUserDefaults standardUserDefaults] setValue:@(YES) forKey:@"isFromMianPage_GotoMyCharge"];
        }
            break;
        case 66663:
        {
            //订单总数
            [self.rdv_tabBarController setSelectedIndex:1];
        }
            break;
            
        default:
            break;
    }
}

-(void)msgAction:(UIButton *)sender {
    BCMessageViewController *nextViewController = [[BCMessageViewController alloc] init];
    [self.navigationController pushViewController:nextViewController animated:YES];
}

-(void)rightAction:(UIButton *)sender {
    NSLog(@"right action");
}
@end
